package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    //根据用户名查询数据库获取用户信息和关联角色信息，同时需要查询角色关联的权限信息
    @Override
    public User findByUserName(String username) {
        //1.查询用户的基本信息，不包含用户的角色
        User user = userDao.findByUserName(username);
        if (user==null) {
            return null;
        }
        Integer userId = user.getId();
        //2.根据用户id查询对应的角色
        Set<Role> roles = roleDao.findByUserId(userId);
        for (Role role : roles) {
            //3.根据角色查询关联权限
            Integer roleId = role.getId();
            Set<Permission> permissions = permissionDao.findByRoleId(roleId);
            //让角色关联权限
            role.setPermissions(permissions);
        }
        //4.让用户关联角色
        user.setRoles(roles);
        return user;
    }

    //获取某用户的菜单集合
    @Override
    public Set<Menu> getMenus(String roleKey) {
        Set<Menu> menus = roleDao.getMenus(roleKey);
        /*if (menus!=null) {
            for (Menu menu : menus) {
                setMenu(menu);
            }
            return menus;
        }*/
        return menus;
    }

    //添加子级菜单
    @Override
    public void setMenu(Menu menu){
        //menu.setChildren(roleDao.getChildrenMenus(menu.getId()));
        List<Menu> childMenus=roleDao.getChildrenMenus(menu.getId());
        if(childMenus!=null&&childMenus.size()>0){
            menu.setChildren(childMenus);
            for (Menu childMenu : childMenus) {
                setMenu(childMenu);
            }
        }
    }

    //获取用户信息
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<User> checkGroups = userDao.selectByCondition(queryString);
        return new PageResult(checkGroups.getTotal(),checkGroups.getResult());
    }

    //保存用户以及与角色的关联关系
    @Override
    public void saveUserAndRole(User user, Integer[] roleIds) {
        userDao.saveUser(user);
        if(roleIds!=null&&roleIds.length>0){
            setItemAndGroup(user, roleIds);
        }
    }

    //删除选中id用户cff
    @Override
    public Result delete(Integer id) {
        //先删除用户与角色的关联关系
        try {
            userDao.delete(id);
            return new Result(true,"用户删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"该用户有角色关联，不能删除!");
        }
    }

    //回显用户数据
    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    //回显用户与角色的关联关系
    @Override
    public List<Integer> getUserAndRole(Integer id) {
        return userDao.getUserAndRole(id);
    }

    //修改用户与角色的关联关系
    @Override
    public void edit(User user, Integer[] roleIds) {
        //先删除用户与角色的关联关系
        userDao.delUserAndRole(user.getId());
        //修改用户信息
        userDao.update(user);
        //注入新的关联关系
        if(roleIds!=null&&roleIds.length>0){
            setItemAndGroup(user, roleIds);
        }
    }

    //修改密码
    @Override
    public void setpassword(User user) {
        userDao.setpassword(user);
    }

    private void setItemAndGroup(User user, Integer[] roleIds) {
        //存储关联关系
        for (Integer roleId : roleIds) {
            Map map = new HashMap();
            map.put("roleId",roleId);
            map.put("userId",user.getId());
            userDao.saveUserAndRole(map);
        }
    }
}
